// pages/academy/academy.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    currentIndex: -1,
    pageSize: 10,
    pageNum: 1,
    schoolName: '',
    academy: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let certificateId = options.certificateId;
    let schoolName = ''
    that.setData({
      certificateId: certificateId,
    })
    that.getData(certificateId, schoolName, that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clickShowHide(e) {
    let that = this;
    // console.log(e);
    let index = e.currentTarget.dataset.index;
    let currentIndex = that.data.currentIndex;
    if (currentIndex == -1) {
      currentIndex = index
    } else if (currentIndex != -1 && currentIndex != index) {
      currentIndex = index
    } else if (currentIndex != -1 && currentIndex == index) {
      currentIndex = -1
    }
    that.setData({
      currentIndex: currentIndex
    })
  },
  getData(certificateId, schoolName, that) {
    let pageSize = that.data.pageSize;
    let pageNum = that.data.pageNum;
    let academy = that.data.academy;
    if ((pageNum-1) * academy.pageSize < academy.total ||  !academy.result) {
      app.globalData.wxRequest.requestGet('school/getListByCertificate.do', {
        certificateId: certificateId,
        pageNum: pageNum,
        pageSize: pageSize,
        schoolName: schoolName
      }).then(res => {
        // console.log(res);
        if(res.code == '201'){
          wx.showToast({
            title: res.msg,
            icon:'error'
          })
        }
        let academy = that.data.academy;
        if (!academy.result) {
          academy = res.data
        } else {
          let list = academy.result.concat(res.data.result);
          // console.log('list',list);
          academy.result = list
          academy.pageNum = res.pageNum
          academy.total = res.total
          academy.pageSize = res.pageSize
        }
        let pageSize = that.data.pageSize;
        pageNum++;
        that.setData({
          academy: academy,
          pageNum: pageNum,
          pageSize: pageSize,
        })
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: '请求失败',
          icon: 'error'
        })
      })
    }else {
      wx.showToast({
        title: '无更多数据',
        icon: 'error'
      })
    }

  },
  scrollgetData() {
    let that = this;
    let certificateId = that.data.certificateId;
    let schoolName = that.data.schoolName;
    that.getData(certificateId, schoolName, that)
  },
  searchSchool() {
    let that = this;
    let certificateId = that.data.certificateId;
    let schoolName = that.data.schoolName;
    that.getData(certificateId, schoolName, that)
  },
  naToschool(e){
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/schoolinfo/schoolinfo?id='+id,
    })
  }
})